import Vue from "vue";
import VueRouter from "vue-router";
import Home from "../views/Home.vue";
import Product from "../views/Product.vue"

Vue.use(VueRouter);

const routes = [
  {
    path: "/",
    redirect:"/home"
  },
  {
    path: "/home",
    name: "home",
    component: Home,
    meta:{
      title:'首页'
    }
  },
  {
    path: '/topic',
    name: 'topic',
    component: () =>
      import(/* webpackChunkName: "Topic" */ "../views/Topic.vue"),
    meta:{
      title:'专题'
    }
  },
  {
    path: '/classify',
    name: 'classify',
    component: () =>
      import(/* webpackChunkName: "Classify" */ "../views/Classify.vue"),
      meta:{
        title:'分类'
      }
  },
  {
    path: '/cart',
    name: 'cart',
    component: () =>
      import(/* webpackChunkName: "Cart" */ "../views/Cart.vue"),
      meta:{
        title:'购物车'
      }
  },
  {
    path: '/user',
    name: 'user',
    component: () =>
      import(/* webpackChunkName: "User" */ "../views/User.vue"),
      meta:{
        title:'我的'
      }
  },
  {
    path: '/product/:id',
    name: 'product',
    props:true,
    component: Product,
    meta:{
      title:'商品详情'
    }
  },
];

const router = new VueRouter({
  mode:'hash',
  routes,
});

router.beforeEach(async (to,from,next) => {
  if (to.meta.title) { // 判断是否有标题
    document.title = to.meta.title;
  }
  next();
});

export default router;
